import {url} from "/baseUrl.js"
import {md5} from "/md5.js"
//请求封装
const secretKey ='rlke@1$'//加密字符串
const version = '1.3.0'//版本号

export function fetch(urls,params,type='GET',header,async=false){
	if(header.typeof == null){
	  let dataTime = ''
	  let strTime = ''
	  let token = ''
	  dataTime = new Date().getTime();
	  strTime = secretKey+dataTime;
	  header = {
		'sign':md5.hex_md5(strTime).toUpperCase(),
		'timestamp':dataTime,
		'token':wx.getStorageSync('token'),
		'version':version,
		'bizCode':'wx'
	  }
	}
	return new Promise((resolve,reject) => {
		wx.showLoading({
			title:'加载中...'
		})
		wx.request({
		  url:url.BASEURL+urls, //仅为示例，并非真实的接口地址
		  data: params,
		  method:type,
		  header: header,
		  timeout:15000,
		  success (res) {
			  wx.hideLoading()
			  if(res.data.errno === 703 
			  || res.data.errno === 402 
			  || res.data.errno === 701 
			  || res.data.errno === 40001
			  || res.data.errno === 708
			  || res.data.errno === -1){
				  wx.showToast({
					  title:res.data.errmsg
				  })
			  }else if(res.data.errno === 501){
				  wx.navigateTo({
					url: "/pages/login/login",
				  })
			  }else if(res.data.errno === 0){
				  resolve(res)
				  reject(0)
			  }else{
				  wx.showToast({
				  	title:res.data.errmsg
				  })
			  }
		  },
		  fail(res){
			  wx.showToast({
			  	title:'请求失败'
			  })
		  }
		})
	})
}